package LeetCode.leetcode47;

import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<Integer> path = new ArrayList<>();
        List<List<Integer>> res = new ArrayList<>();
        backTracking(path,res,nums,new boolean[nums.length],nums.length);
        return res;
    }
    public static void backTracking(List<Integer> path, List<List<Integer>> res,int[] nums,boolean[] used,int index){
        if (path.size() == nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i-1] && !used[i-1] || i == index) continue;
            path.add(nums[i]);
            used[i] = true;
            backTracking(path, res, nums,used,i);
            used[i] = false;
            path.remove(path.size()-1);
        }
    }
}
